//
//  LSZAudioRViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/4/13.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZAudioRViewController.h"

@interface LSZAudioRViewController ()

@end

@implementation LSZAudioRViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setAudioSearchUI];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.historyMutArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (ScreenWidth == 320) {
        //5
        return 45;
    }else if(ScreenWidth == 375){
        // 6 7
        return 50;
    }
    else {
        //plus
        return 58;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (ScreenWidth == 320) {
        //5
        return 35;
    }else if(ScreenWidth == 375){
        // 6 7
        return 40;
    }
    else {
        //plus
        return 48;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerCell"];
    if (!cell) {
        cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"headerCell"];
    }
    if (tableView == self.tableViewHistorical) {
        cell.textLabel.text = @"历史记录";
        cell.textLabel.textColor = RGBColor(139, 139, 139);
        [LSZSpecialPackaging setLabelAdaptiveSize:cell.textLabel font:13];
    }else{
        cell.textLabel.hidden = YES;
    }
    return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZSearchHistoricalTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZSearchHistoricalTableViewCell" forIndexPath:indexPath];
    Person *person = self.historyMutArray[indexPath.row];
    cell.labelTitle.text = [NSString stringWithFormat:@"%@",person.name];
    cell.butDeteting.tag = indexPath.row+1;
    cell.imageViewDeteting.tag =  indexPath.row+1;
    [cell.butDeteting addTarget:self action:@selector(clickButDetatle:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark -  dianji  -
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Person *person = self.historyMutArray[indexPath.row];
    self.countPage = 1;
    self.textFieldSearch.text = person.name;
    [self initWithDataCountStr:[NSString stringWithFormat:@"%@",person.name]  :self.countPage];
    [self initesignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}
-(void)goToLogin
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"AccLogin" bundle:nil];
    UIViewController *controller = [sb instantiateInitialViewController];
    [self.navigationController pushViewController:controller animated:YES];
}
-(void)setAudioSearchUI
{
    [self.navigationController.navigationItem setHidesBackButton:YES];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationController.navigationBar.backItem setHidesBackButton:YES];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home_nav_button_back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];

    UIButton * butCancel = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    [butCancel setTitle:@"搜索" forState:UIControlStateNormal];
    [butCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [butCancel addTarget:self action:@selector(clickCancelBut) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:butCancel];

    self.view.backgroundColor = RGBColor(236, 236, 236);

    UIView * viewSearch = [[UIView alloc]initWithFrame:CGRectMake( 50, NAVIGATION_BAR_HEIGHT-37, ScreenWidth-80-30, 35)];
    viewSearch.backgroundColor = [UIColor whiteColor];
    [viewSearch.layer setMasksToBounds:YES];
    [viewSearch.layer setCornerRadius:5];
    viewSearch.userInteractionEnabled = YES;
    self.navigationItem.titleView = viewSearch;

    UIImageView * imageViewSearch = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 18, 18)];
    imageViewSearch.image = [UIImage imageNamed:@"灰色搜索"];
    [viewSearch addSubview:imageViewSearch];

    UITextField * textFieldSearch = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageViewSearch.frame)+5, 5,viewSearch.frame.size.width-CGRectGetMaxX(imageViewSearch.frame)-13,30)];
    self.textFieldSearch = textFieldSearch;
    textFieldSearch.backgroundColor = [UIColor whiteColor];
    textFieldSearch.delegate = self;
    textFieldSearch.tintColor = [UIColor redColor];
    textFieldSearch.placeholder = @"搜索音频,赞美诗...";
    textFieldSearch.clearButtonMode = UITextFieldViewModeAlways;
    [viewSearch addSubview:textFieldSearch];
}
//点击return 按钮 去掉
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self clickCancelBut];
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * str = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if(str.length == 0 ){
        [self PopularAndHistoricalRecordsAreNOHidden];
    }else{
        if (self.collectionView.hidden == YES&&self.tableViewHistorical.hidden == YES ) {
            return YES;
        }else{
            [self PopularAndHistoricalRecordsAreHidden];
        }
    }
    return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    [self PopularAndHistoricalRecordsAreNOHidden];
    return YES;
}
#pragma mark - 键盘
-(void)initesignFirstResponder
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
#pragma mark - 搜索结果隐藏
-(void)SearchResultsHidden
{
    self.viewTop.hidden = YES;
}
#pragma mark - 热门和历史记录隐藏
-(void)PopularAndHistoricalRecordsAreHidden
{
    self.collectionView.hidden = YES;
    self.tableViewHistorical.hidden = YES;
}
-(void)PopularAndHistoricalRecordsAreNOHidden
{
    self.collectionView.hidden = NO;
    self.tableViewHistorical.hidden = NO;
    [self.tableViewHistorical reloadData];
}
#pragma mark - 搜索按钮的点击事件
-(void)clickCancelBut
{
    if (self.textFieldSearch.text.length > 0) {
        self.countPage = 1;
        //加入历史记录
        Person *person = [[Person alloc] init];
        person.name = self.textFieldSearch.text;
        [[LCMusicDataBase sharedDataBase] addPerson:person];
        self.historyMutArray = [[LCMusicDataBase sharedDataBase] getAllPerson];
        self.historyMutArray = (NSMutableArray *)[[self.historyMutArray reverseObjectEnumerator] allObjects];
        [self.tableViewHistorical reloadData];
        //去下个界面
         [self initWithDataCountStr:self.textFieldSearch.text :1];
    }else{
        [SVProgressHUD showErrorWithStatus:@"请输入您要搜索的关键词!"];
        [SVProgressHUD dismissWithDelay:1];
    }
}
#pragma mark - 点击的删除的那个
-(void)clickButDetatle:(UIButton *)but
{
    Person *person = self.historyMutArray[but.tag-1];
    [[LCMusicDataBase sharedDataBase] deletePerson: person];
    self.historyMutArray = [[LCMusicDataBase sharedDataBase] getAllPerson];
    self.historyMutArray = (NSMutableArray *)[[self.historyMutArray reverseObjectEnumerator] allObjects];
    [self.tableViewHistorical reloadData];
}
#pragma mark  - 选择的是那个
-(void)clickButSeare:(UIButton *)but {
    [self initesignFirstResponder];
    if (self.clickChoose == but.tag) {
        return;
    }
    self.clickChoose = but.tag;
    self.countPage = 1;
    [self initWithDataCountStr:self.textFieldSearch.text :self.countPage];
    CGRect rect = self.labelRed.frame;
    rect.origin.x = (but.tag)%5*60;
    self.labelRed.frame = rect;
}
#pragma mark - 导航的返回键
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
